//https://leetcode.cn/problems/longest-palindromic-substring/

class Solution {
public:
    string longestPalindrome(string s) {
        //记录返回字符串的起始位置和长度
        int begin = 0, len = 0;
        //以每一个字符为中点枚举
        for (int i = 0; i < s.size(); i++)
        {
            //寻找奇数长度的子串
            int left = i, right = i;
            while (left >= 0 && right < s.size() && s[left] == s[right])
            {
                left--;
                right++;
            }

            if (right - left - 1 > len)
            {
                begin = left + 1;
                len = right - left - 1;
            }

            //寻找偶数长度的子串
            left = i;
            right = i + 1;
            while (left >= 0 && right < s.size() && s[left] == s[right])
            {
                left--;
                right++;
            }

            if (right - left - 1 > len)
            {
                begin = left + 1;
                len = right - left - 1;
            }

        }

        string ret = s.substr(begin, len);
        return ret;

    }
};